<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController {
    public function lst(){
		$article=D('article');
		$count      = $article->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$Page->setConfig('first','首页');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('end','共有{%TOTAL_PAGE%}页');
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$lis= $article->field('a.id,a.title,b.cate_name')->alias('a')->join('LEFT JOIN sp_category b ON a.cateid=b.id')->limit($Page->firstRow.','.$Page->listRows)->select();
		//dump($lis);die;
		$this->assign('lis',$lis);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display('lst');
	}
	
	
	public function del(){
		$mod=D('article');
		if($mod->delete(I('id'))){
			$this->success("文章删除成功！",U('lst'));
		}else{
			$this->error("文章删除失败！");
		}
	}
	

	
	public function add() {
				$article = M('article'); //实例化数据库
				if (!empty($_POST)) {
					$article->create(); //收集post表单数据
					$str = $article->add(); //添加数据
				if ($str) {
							$this->success('文章添加成功！', U('lst'));
				} else {
							$this -> error('文章添加失败！', U('add'));
				}
				return;
			} 
			
			$categoryres=M('category')->select();
			$this->assign('categoryres',$categoryres);
			$this->display();
	}
	
	
	
	
	public function edit(){
		$article=M('article');
		if(IS_POST){
			if($article->create()){
				if($article->save()){
					$this->success("修改文章成功！",U('lst'));
				}else{
					$this->error("修改文章失败！");
				}
			}
			return ;
		}
		$categoryres=M('category')->select();
		$this->assign('categoryres',$categoryres);
		$articles = $article->find(I('id'));
		$this->assign("articles",$articles);
		$this->display();
		
	}
}